﻿using System;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows.Media;
using Bookvar.Common;

namespace Bookvar.UI.Converters
{
	public class FileNameToImageSourceConverter : BookvarConverter<FileNameToImageSourceConverter>, IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			string fileName = null;
			string stringValue = value as string;
			if (value != null)
			{
				fileName = stringValue;
			}
			else
			{
				FileInfo info = value as FileInfo;
				if (value != null)
				{
					fileName = info.FullName;
				}
			}
			if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName))
			{
				return FileUtility.GetFileIcon(fileName);
			}
			return null;
		}

		public object ConvertFrom(object value)
		{
			return Convert(value, typeof(ImageSource), null, CultureInfo.InvariantCulture);
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}
}